<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Order
 *
 * @author Sorasak Siangsanan
 */
class Order extends CActiveRecord {
    
    public $orderOwnerName = '';

    public static function model($className = __CLASS__) {
        return parent::model($className);
    }

    public function primaryKey() {
        return 'order_id';
    }

    public function tableName() {
        return "order";
    }

    public function attributeLabels() {
        return array(
            "order_id" => Yii::t('order_model', 'order_id'),
            "cust_id" => Yii::t('order_model', 'cust_id'),
            "status_id" => Yii::t('order_model', 'status'),
            "package_id" => Yii::t('order_model', 'package_id'),
            "product_id" => Yii::t('order_model', 'product_id'),
            "option_type" => Yii::t('order_model', 'option_type'),
            "price" => Yii::t('order_model', 'price'),
            "order_date" => Yii::t('order_model', 'order_date'),
            "name" => Yii::t('order_model', 'name'),
            "surname" => Yii::t('order_model', 'surname'),
            "birthdate" => Yii::t('order_model', 'birthdate'),
            "sex" => Yii::t('order_model', 'sex'),
            "height" => Yii::t('order_model', 'height'),
            "weight" => Yii::t('order_model', 'weight'),
            "picture" => Yii::t('order_model', 'picture'),
            "contact_tel" => Yii::t('order_model', 'contact_tel'),
            "remark" => Yii::t('order_model', 'remark'),
            "create_date" => Yii::t('order_model', 'create_date'),
            "update_date" => Yii::t('order_model', 'update_date'),
        );
    }

    public function relations() {
        return array(
            'customer' => array(self::BELONGS_TO, 'Customer', 'cust_id'),
            'status' => array(self::BELONGS_TO, 'Status', 'status_id'),
            'package' => array(self::BELONGS_TO, 'Package', 'package_id'),
            'payment' => array(self::BELONGS_TO, 'Payment', 'order_id')
        );
    }

    public function rules() {
        return array(
            array('order_id, cust_id, status_id', 'safe', 'on' => 'search'),
            array('order_id, name, surname', 'safe', 'on' => 'searchUser')
        );
    }

    public function search() {
        $criteria = new CDbCriteria;

        $criteria->with = array('customer');
        $criteria->with = array('status');

        $criteria->compare('t.order_id', $this->order_id, false);

        if (isset($this->cust_id)) {
            $customers = Customer::model()->findAll(array('condition' => 'Name like \'%' . $this->cust_id . '%\' or Surname like \'%' . $this->cust_id . '%\''));
            $custId = array();
            foreach ($customers as $customer) {
                array_push($custId, $customer->id);
            }
            $criteria->addInCondition('t.cust_id', $custId);
        }

        if (isset($this->status_id)) {
            $status_list = Status::model()->findAll(array('condition' => 'description like \'%' . $this->status_id . '%\''));
            $statusId = array();
            foreach ($status_list as $status) {
                array_push($statusId, $status->status_id);
            }
            $criteria->addInCondition('t.status_id', $statusId);
        }

        return new CActiveDataProvider(get_class($this), array(
            'criteria' => $criteria,
            'pagination' => array(
                'pageSize' => 10
            ),
            'sort' => array(
                'defaultOrder' => 'create_date desc',
                'multiSort' => true,
            ),
                )
        );
    }

    public function searchUserEdit() {
        $criteria = new CDbCriteria;

        $criteria->with = array('customer');
        $criteria->with = array('status');

        $criteria->compare('t.order_id', $this->order_id, false);
        $criteria->compare('t.name', $this->name, true);
        $criteria->compare('t.surname', $this->surname, true);
        $criteria->compare('t.cust_id', Yii::app()->user->id, false);
        $criteria->addInCondition('t.status_id', array(1, 2));

        if (isset($this->cust_id)) {
            $customers = Customer::model()->findAll(array('condition' => 'Name like \'%' . $this->cust_id . '%\' or Surname like \'%' . $this->cust_id . '%\''));
            $custId = array();
            foreach ($customers as $customer) {
                array_push($custId, $customer->id);
            }
            $criteria->addInCondition('t.cust_id', $custId);
        }

        if (isset($this->status_id)) {
            $status_list = Status::model()->findAll(array('condition' => 'description like \'%' . $this->status_id . '%\''));
            $statusId = array();
            foreach ($status_list as $status) {
                array_push($statusId, $status->status_id);
            }
            $criteria->addInCondition('t.status_id', $statusId);
        }

        return new CActiveDataProvider(get_class($this), array(
            'criteria' => $criteria,
            'pagination' => array(
                'pageSize' => 10
            ),
            'sort' => array(
                'defaultOrder' => 'create_date desc',
                'multiSort' => true,
            ),
                )
        );
    }

    public function searchUserFinish() {
        $criteria = new CDbCriteria;

        $criteria->with = array('customer');
        $criteria->with = array('status');

        $criteria->compare('t.order_id', $this->order_id, false);
        $criteria->compare('t.name', $this->name, true);
        $criteria->compare('t.surname', $this->surname, true);
        $criteria->compare('t.cust_id', Yii::app()->user->id, false);
        $criteria->addNotInCondition('t.status_id', array(1, 2));


        return new CActiveDataProvider(get_class($this), array(
            'criteria' => $criteria,
            'pagination' => array(
                'pageSize' => 10
            ),
            'sort' => array(
                'defaultOrder' => 'create_date desc',
                'multiSort' => true,
            ),
                )
        );
    }

    public function searchForward() {
        $criteria = new CDbCriteria;

        $criteria->with = array('customer');
        $criteria->with = array('status');

        $criteria->compare('t.order_id', $this->order_id, false);
        $criteria->compare('t.status_id', 3, false);

        if (isset($this->cust_id)) {
            $customers = Customer::model()->findAll(array('condition' => 'Name like \'%' . $this->cust_id . '%\' or Surname like \'%' . $this->cust_id . '%\''));
            $custId = array();
            foreach ($customers as $customer) {
                array_push($custId, $customer->id);
            }
            $criteria->addInCondition('t.cust_id', $custId);
        }

        return new CActiveDataProvider(get_class($this), array(
            'criteria' => $criteria,
            'pagination' => array(
                'pageSize' => 10
            ),
            'sort' => array(
                'defaultOrder' => 'create_date asc, price desc',
                'multiSort' => true,
            ),
                )
        );
    }

}
